//
//  CBJWelComeController.swift
//  Weibo
//
//  Created by 崔健 on 16/5/23.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit
import SDWebImage

class CBJWelComeController: UIViewController {

    var userAccount : CBJUserAccount?
    
    var iconImageView : UIImageView = UIImageView(image: UIImage.init(named: "avatar_default_big"))
    var sinaImageView : UIImageView = UIImageView(image: UIImage.init(named: "ad_background"))
    var nameLabel : UILabel = {
    
        let label = UILabel()
        label.text = CBJUserAccountViewModel.sharedAccountViewModel.userAccount?.name
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }

    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        
        let offset = 160 - kScreenHeight
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 4, options: [], animations: {

            self.iconImageView.snp_updateConstraints(closure: { (make) in
                
//                make.bottom.equalTo(self.view).offset()
                make.bottom.equalTo(offset)
                
            })
            
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
                UIView.animateWithDuration(0.5, animations: {
                    
                    self.nameLabel.alpha = 1
                    
                    }, completion: { (_) in
                        
                        // 发送通知切换根视图控制器
                        NSNotificationCenter.defaultCenter().postNotificationName("changeRootVc", object: "main")
                })
        }
    }
    
    
    func setupUI(){
    
        view.addSubview(sinaImageView)
        view.addSubview(iconImageView)
        view.addSubview(nameLabel)
        
        sinaImageView.snp_makeConstraints { (make) in
            
            make.left.top.right.bottom.equalTo(view)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            make.height.width.equalTo(90)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(iconImageView.snp_bottom).offset(8)
            make.centerX.equalTo(iconImageView)
        }

        iconImageView.sd_setImageWithURL(NSURL(string: CBJUserAccountViewModel.sharedAccountViewModel.userAccount?.avatar_large ?? "avatar_large"))
        
        iconImageView.layer.cornerRadius = 45
        iconImageView.layer.masksToBounds = true
        nameLabel.alpha = 0
        
    }
    
    override func prefersStatusBarHidden() -> Bool {
        
        return true
    }
  
}
